
let SpriteAnimator = function (painters, elapsedCallback) {
    this.painters = painters;
    if (elapsedCallback) {
        this.elapsedCallback = elapsedCallback;
    }
};

SpriteAnimator.prototype = {
    painters: [],
    duration: 1000,
    startTime: 0,
    index: 0,
    elapsedCallback: undefined,

    end: function (sprite, originalPainter) {
        sprite.animating = false;

        if (this.elapsedCallback) {
            this.elapsedCallback(sprite);
        }
        else {
            sprite.painter = originalPainter;
        }
    },

    start: function (sprite, duration) {
        let endTime = +new Date() + duration,
            period = duration / (this.painters.length),
            interval = undefined,
            animator = this, // for setInterval() function
            originalPainter = sprite.painter;

        this.index = 0;
        sprite.animating = true;
        sprite.painter = this.painters[this.index];

        interval = setInterval(function() {
            if (+new Date() < endTime) {
                sprite.painter = animator.painters[++animator.index];
            }
            else {
                animator.end(sprite, originalPainter);
                clearInterval(interval);
            }
        }, period);
    },
};
